<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Redis;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Deal;
use App\Car;
use App\Bargain;
use App\Seller;
use App\User;

class BuycarController extends Controller
{
    /**
    * 在这里定义这些属性
    */
                public $citys = '';
                public $brand_one = '';
                public $brand_two = '';
                public $brand_three = '';

    /**
    * 构造方法  自动加载配置文件
    */
    public function __construct()
    {
                $this->citys = config('city');
                $this->brand_one = config('buybrand1');
                $this->brand_two = config('buybrand2');
                $this->brand_three = config('buybrand3');
    }

    /**
    * 获取中文地理位置
    */
    public function getCity(Request $req)
    {
                /*获取到拼音的地理位置*/
                $city = $req->city;
                /*截取第一个字母转化成大写*/
                $head = strtoupper(substr($city,0,1));
                /*引入配置文件*/
                $citys = config('city');
                /*获取到中文城市名,然后再赋值到模板上面*/
                $china_city = $citys[$head][$city];
                /*最后把这个得到的 $china_city 返回出去*/
                return $china_city;
    }
    /**
    * 点击车子详情跳转方法
    */
    public function details(Request $req)
    {
                /*获取到地理位置的拼音*/
                $city = $req->city;
                /*然后再调用本类里面的方法得到中文的地理位置*/
                $china_city = $this->getCity($req);
                /*获取用户点击图片car_id*/
                $car_id = $req->car_id;
                // dd($car_id);
                /*在根据用户点击传过来的car_id,在数据库里进行查找*/
                $info = DB::table('seller')
                    ->where(['seller.car_id'=>$car_id,'car.sale_status'=>1])
                    ->join('car', 'seller.car_id', '=', 'car.car_id')
                    ->join('survey', 'seller.car_id', '=', 'survey.car_id')
                    ->join('appraiser', 'car.app_id', '=', 'appraiser.app_id')
                    ->select('car.*','seller.*','survey.*','appraiser.*')
                    ->first();
                $series = DB::table('series')
                       ->where(['series_name'=>$info->series_name])
                       ->select('series_pinyin','brand_id')
                       ->first();
                $series_pinyin = $series->series_pinyin;
                $brand_id = $series->brand_id;
                $brand = DB::table('brand')->where('brand_id',$brand_id)->select('brand_pinyin')->first();
                $brand_pinyin = $brand->brand_pinyin;
                // $brand_pinyin =
                $imgs = DB::table('survey')
                    ->where('car_id','=',$car_id)
                    ->select('other_img_name','other_img_path')
                    ->first();
                    // dd($imgs);
                /*explode返回由字符串组成的数组,以第一个给定的参数分割出来*/
                /*trim去除首尾处的空白字符*/
                $arr = explode(' ',trim($imgs->other_img_name));
                //   dd($imgs->other_img_path);  "/images/imgche"
                $otherimgs = [];
                foreach ($arr as $key => $value) {
                    // dd($imgs->other_img_path.$value); 出来的是一个完整的图片路径
                      array_push($otherimgs, $imgs->other_img_path.$value);
                      /*返回一个这样的数组*/
                    //   Array ( [0] => /images/imgche/201707128161.jpg )
                }
                // dd($otherimgs);   返回一个索引数字
            return view('detail',['info'=>$info,'china_city'=>$china_city,'city'=>$city,'otherimgs'=>$otherimgs,'citys'=>$this->citys,'series_pinyin'=>$series_pinyin,'brand_pinyin'=>$brand_pinyin]);
    }

    /**
     * 用户提交上砍价信息
     */
    public function postPrice(Request $req)
    {

                $uid = $req->user()->uid;
                $bargain = new Bargain();
                $bargain->car_id = $req->car_id;
                $bargain->cut_money = $req->cut_money;
                $bargain->uid = $uid;
                $bargain->orig_price = $req->price;
                $bargain->sell_id = $req->sell_id;
                if ($bargain->save()) {
                    $content = '您的砍价已经提交,我们会和卖家取得联系,请保持电话畅通!';
                    return view('success',['content'=>$content]);
                }
    }

//车辆详情页点击预约看车
    public function  yykc(Request $req)
    {

                $deal=new Deal();
                $deal->uid = $req->user()->uid;
                $deal->car_id = $req->car_id;
                $deal->sell_id = $req->sell_id;
                $deal->deal_price = $req->price;
                $deal->deal_number= date('YmdHis').mt_rand(10000,99999);
                $deal->pubtime=date('Ymd Hi');
                if($deal->save()){
                    DB::table('car')->where('car_id',$req->car_id)->update(['sale_status'=>0]);
                    $content = '恭喜您买到心仪的车辆,感谢您对瓜子网的支持,祝您生活愉快!';
                    return view('success',['content'=>$content]);
                }

    }

    //砍价情况点击预约看车
    public function  appoint(Request $req)
    {
                // return $req->all();
                $deal=new Deal();
                $deal->sell_id = $req->user()->uid;
                $deal->car_id = $req->car_id;
                $deal->uid = $req->uid;
                $deal->deal_price = mb_substr($req->cut_money,0,-1);
                $deal->deal_number= date('YmdHis').mt_rand(10000,99999);
                $deal->pubtime=date('Ymd Hi');
                if($deal->save()){
                    DB::table('bargain')->where('bar_id',$req->bar_id)->update(['status'=>1]);
                    DB::table('car')->where('car_id',$req->car_id)->update(['sale_status'=>0]);

                }

    }
    /**
     * 根据价格的查找
     */
     public function carPrice($price,$china_city)
     {
                /*将传过来的值当作数组的键*/
                $arr = [
                    'p3'  => [0,5],
                    'p4'  => [5,10],
                    'p5'  => [10,15],
                    'p6'  => [15,20],
                    'p7'  => [20,25],
                    'p8'  => [25,40],
                    'p9'  => [40,60],
                    'p10' => [60,100]
                ];
                /*再把需要的查询条件传进去*/
                $cars = DB::table('car')
                    ->whereBetween('price',$arr[$price])
                    ->where(['check_status'=>1,'survey_status'=>1,'seller_status'=>1,'checkcar_city'=>$china_city])
                    /*注意这里需要进行两表联查*/
                    ->join('survey', 'car.car_id', '=', 'survey.car_id')
                    ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                    ->paginate(1);
                return $cars;
      }

       /**
        * 从路由当中获取到的拼音车系在这里转化成中文
        */
        public function get_series_name($series_pinyin)
        {
                /*获取到的车系是拼音,需要把它转化成中文*/
                $series_name = DB::table('series')
                    ->where('series_pinyin','=',$series_pinyin)
                    ->select('series_name')
                    ->first();
                /*打印出来是一个对象,所以要把它取出来*/
                $series_name = $series_name->series_name;
                return $series_name;
        }

        /**
         * 将传过来的$price字符串转化成数字范围
         */
         public function get_Price($price)
         {
             $arr = [
                'p3'  => [0,5],
                'p4'  => [5,10],
                'p5'  => [10,15],
                'p6'  => [15,20],
                'p7'  => [20,25],
                'p8'  => [25,40],
                'p9'  => [40,60],
                'p10' => [60,100],
                'p11' => [0,3],
                'p12' => [3,5],
                'p13' => [5,7],
                'p14' => [7,9],
                'p15' => [9,12],
                'p16' => [12,16],
                'p17' => [16,20],
                'p18' => [20,100]
             ];
             return $arr[$price];
         }

          /**
           * 点击价格路由
           */
           public function indexBuyCar(Request $request)
           {
               $city = $request->city;
               $china_city = $this->getCity($request);
               if ($request->pn) {
                   $price = $this->get_Price($request->pn);
                   $cars = DB::table('car')
                        ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'sale_status'=>1])
                        ->whereBetween('price',$price)
                        ->join('survey','car.car_id', '=', 'survey.car_id')
                        ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                        ->paginate(1);
                return view('buycar',['price'=>$request->pn, 'cars'=>$cars,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);

               } else {
                   $cars = DB::table('car')
                        ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'sale_status'=>1])
                        ->join('survey','car.car_id', '=', 'survey.car_id')
                        ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                        ->paginate(1);
                return view('buycar',['cars'=>$cars,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
               }

           }

      /**
       * 点击品牌路由
       */
       public function indexBrand(Request $request)
       {
           $city = $request->city;
           $china_city = $this->getCity($request);
           /*点击品牌跳转方法*/
           if (preg_match("/[a-z]+/", $request->brand)) {
                $brand_pinyin = $request->brand;
                $brandInfo = DB::table('brand')
                     ->where(['brand_pinyin'=>$brand_pinyin])
                     ->select('brand_name')
                     ->first();
                $brand_name = $brandInfo->brand_name;

           } else {
               /*搜索品牌跳转方法*/
                $brand_name = $request->brand;
                $brandInfo = DB::table('brand')
                     ->where(['brand_name'=>$brand_name])
                     ->select('brand_pinyin')
                     ->first();
                $brand_pinyin = $brandInfo->brand_pinyin;
           }
           $brand = DB::table('brand')->where(['brand_name'=>$brand_name])->select('brand_id')->first();
           $brand_id = $brand->brand_id;
           $series = DB::table('series')->where(['brand_id'=>$brand_id])->select('series_name','series_pinyin')->get();
           //dd($series);
           if($request->pn){
               $price = $this->get_Price($request->pn);
               $cars = DB::table('car')
                    ->whereBetween('price',$price)
                    ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'sale_status'=>1])
                    ->join('survey','car.car_id', '=', 'survey.car_id')
                    ->where('brand_name','=',$brand_name)
                    ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                    ->paginate(1);
                return view('buycar',['series'=>$series,'price'=>$request->pn,'brand'=>$brand_pinyin,'cars'=>$cars,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
           } else {
               $cars = DB::table('car')
                    ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'sale_status'=>1])
                    ->join('survey','car.car_id', '=', 'survey.car_id')
                    ->where('brand_name','=',$brand_name)
                    ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                    ->paginate(1);
                return view('buycar',['series'=>$series,'cars'=>$cars,'brand'=>$brand_pinyin,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
           }

       }

      /**
       * 点击车系路由
       */
       public function indexSeries(Request $request)
       {
           $city = $request->city;
           $china_city = $this->getCity($request);
           $series_pinyin = $request->series;
           $series_name = $this->get_series_name($series_pinyin);

           if($request->pn){
               $price = $this->get_Price($request->pn);
               $cars = DB::table('car')
                    ->whereBetween('price',$price)
                    ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'series_name'=>$series_name])
                    ->join('survey','car.car_id', '=', 'survey.car_id')
                    ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                    ->paginate(1);

                return view('buycar',['price'=>$request->pn,'series_pinyin'=>$series_pinyin,'cars'=>$cars,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
           } else {
               $cars = DB::table('car')
                    ->where(['checkcar_city'=>$china_city,'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'series_name'=>$series_name])
                    ->join('survey','car.car_id', '=', 'survey.car_id')
                    ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                    ->paginate(1);
                return view('buycar',['series_pinyin'=>$series_pinyin,'cars'=>$cars,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
           }
       }

    public function ajaxBrand(Request $request)
    {
        //获取到请求的品牌
        // return $request->brand;
        $brand_name = trim($request->brand);
        $brandInfo = DB::table('brand')
                     ->where(['brand_name'=>$brand_name])
                     ->first();
        if ($brandInfo) {
            $brand_pinyin = $brandInfo->brand_pinyin;
            return $brand_pinyin;
        } else {
            return 0;
        }

    }
    /**
     * 用户通过首页的搜索框点击进来
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function keyword(Request $request)
    {
        //获取到模板页中的城市名称和搜索关键词
        $city = $request->city;
        $china_city = $this->getCity($request);
        $brand_name = $request->keyword;
        $brand = DB::table('brand')
                             ->where('brand_name','=',$brand_name)
                             ->select('brand_pinyin')
                             ->first();
        $brand_pinyin = $brand->brand_pinyin;
        $cars = DB::table('car')
                ->where(['checkcar_city'=>$china_city, 'check_status'=>1,'survey_status'=>1,'seller_status'=>1,'brand_name'=>$brand_name])
                ->join('survey','car.car_id', '=', 'survey.car_id')
                ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path')
                ->paginate(1);
        return view('buycar',['cars'=>$cars,'brand'=>$brand_pinyin,'city'=>$city,'china_city'=>$china_city,'brand_one'=>$this->brand_one,'brand_two'=>$this->brand_two,'brand_three'=>$this->brand_three,'citys'=>$this->citys]);
    }


}
